package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.log.MyLog;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private HttpServletRequest request;

    /**
     * 员工登录
     */
    @PostMapping("/login")
    @MyLog("员工登录")
    public R<Employee> getLogin(@RequestBody Employee employee) {
        //1.根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(lqw);

        //判断用户名是否存在
        if (emp == null) {
            return R.error("登录失败，用户名或密码错误！");
        }

        //页面提交的密码进行MD5加密
        String password = employee.getPassword();
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        //判断密码是否正确
        if (!emp.getPassword().equals(md5Password)) {
            return R.error("登录失败，用户名或密码错误！");
        }
        //查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (emp.getStatus() == 0) {
            return R.error("账号已禁用！");
        }

        //登陆成功，将员工id存入session并返回成功登录的结果
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);

    }

    /**
     * 员工退出
     * @return
     */
    @PostMapping("/logout")
    @MyLog("员工退出")
    public R<String> getLogout() {
        //清理session中保存的当前登录员工的id
        request.getSession().removeAttribute("employee");
        return R.success("登出成功！");

    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public R<Page> getPage(Integer page, Integer pageSize, String name) {
        //分页查询
        Page<Employee> page1 = new Page<>(page, pageSize);
        //模糊查询
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        //按修改时间倒序排列
        wrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询
        Page<Employee> employeePage = employeeService.page(page1, wrapper);
        return R.success(employeePage);
    }

    /**
     * 添加员工
     *
     * @param employee
     * @return
     */
    @PostMapping
    @MyLog("添加员工")
    public R<String> save(@RequestBody Employee employee) {
        String password = "123456";
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        employee.setPassword(md5Password);
        employeeService.save(employee);
        return R.success("新增成功!");
    }

    /**
     * 修改状态操作
     *
     * @param employee
     * @return
     */
    @PutMapping
    @MyLog("修改员工状态")
    public R<String> updateStates(@RequestBody Employee employee) {
        log.info("接收到前端修改员工的请求:{}", employee);
        employeeService.updateById(employee);
        return R.success("修改成功!");
    }

    /**
     * 用户信息回显
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
        if (id == null) {
            return R.error("账户查不到!");
        }
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }
}
